#include <iostream>
#include <fstream>
using std::cerr;
using std::cout;
using std::endl;
using std::ifstream;

void test0(){
    ifstream ifs("explicit.cc");

    //文件输入流处于非goodbit状态就进行处理
    /* if(!ifs.good()){ */
    if(!ifs){
        cerr << "ifstream open file failed!" << endl;
        return;
    }

    ifs.seekg(0,std::ios::end);
    long length = ifs.tellg();
    cout << length << endl;

    char * pdata = new char[length + 1]();

    ifs.seekg(0);
    ifs.read(pdata,length);
    cout << pdata << endl;

    delete [] pdata;
    pdata = nullptr;

    ifs.close();
}

void test1(){
    //打开即将文件游标置于结尾
    ifstream ifs("explicit.cc",std::ios::ate);

    //文件输入流处于非goodbit状态就进行处理
    /* if(!ifs.good()){ */
    if(!ifs){
        cerr << "ifstream open file failed!" << endl;
        return;
    }

    //省略了第一次将文件游标置于结尾的操作
    /* ifs.seekg(0,std::ios::end); */
    long length = ifs.tellg();
    cout << length << endl;

    char * pdata = new char[length + 1]();

    ifs.seekg(0);
    ifs.read(pdata,length);
    cout << pdata << endl;

    delete [] pdata;
    pdata = nullptr;

    ifs.close();
}
int main(void){
    test1();
    return 0;
}
